package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hermit.qmst.R;

/**
 * Created by hermithermit on 15/3/31.
 */
public class OpenUrlActivity extends Activity{

    private String TAG = "OpenUrlActivity";
    private WebView myWebView;
    private ProgressBar mpProgressBar;
    private ImageView mBack;
    private ImageView mClose;
    private TextView mtvTitle;
    private String mUrl;
    private String mTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_webview);

        Intent intent = getIntent();
        if (intent == null) {
            finish();
        }else {
            mUrl = intent.getStringExtra("url");
            mTitle = intent.getStringExtra("title");
        }

        myWebView = (WebView) findViewById(R.id.webView1);
        mpProgressBar = (ProgressBar)findViewById(R.id.progressBar11);
        mBack = (ImageView)findViewById(R.id.iv_back);

        mtvTitle = (TextView)findViewById(R.id.tv_title);

        mtvTitle.setText(mTitle);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.setWebChromeClient(new MyWebChromeClient());
        myWebView.getSettings().setSupportZoom(true);
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.getSettings().setAllowFileAccess(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.getSettings().setJavaScriptEnabled(true);

        myWebView.loadUrl(mUrl);

        mBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //myWebView.goBack();
                finish();

            }
        });


    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
			/*if (Uri.parse(url).getHost().equals(getString(R.string.app_weishop_url2))) {

				// This is my web site, so do not override; let my WebView load the page
				//return false;
			}         */
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs

            view.loadUrl(url);

            mpProgressBar.setVisibility(View.VISIBLE);
            return true;
        }
    }
    private class MyWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            // TODO Auto-generated method stub
            super.onProgressChanged(view, newProgress);
            if (newProgress == 100) {
                mpProgressBar.setVisibility(View.GONE);
            }

        }

    }


}
